package cn.akwangl.design.pattern.creational.singleton;

/**
 * 单例模式v2. 双重检查单例,volatile 解决实例化重排序问题.
 * <p>
 * 实例重排序<br/>
 * 1.分配内存给这个对象<br/>
 * 2.初始化对象<br/>
 * 3.设置lazyDoubleCheckSingleton 指向刚分配的内存地址<br/>
 * </p>
 *
 * @author youyongkun
 * @date 2021/11/24 16:18
 */
public class LazyDoubleCheckSingleton {

    private volatile static LazyDoubleCheckSingleton lazyDoubleCheckSingleton = null;

    private LazyDoubleCheckSingleton() {

    }

    public static LazyDoubleCheckSingleton instance() {
        if (lazyDoubleCheckSingleton == null) {
            synchronized (LazyDoubleCheckSingleton.class) {
                if (lazyDoubleCheckSingleton == null) {
                    lazyDoubleCheckSingleton = new LazyDoubleCheckSingleton();
                }
            }
        }
        return lazyDoubleCheckSingleton;
    }
}
